package com.sls.api.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletResponse;

import com.sls.core.socket.TransmissionCode;

/**
 * @Description xml格式响应数据返回
 * @author Wangxuegang
 * @date 2012-09-01
 * 
 */
public class ResponseDataXmlFormat {
	
	/**
	 * 数据校验失败返回值
	 * 
	 * @param resCode
	 * @param resReason
	 * @param resp
	 * @throws IOException
	 */
	public static void returnResMsgOfUTF8(String resCode, String resReason,
			HttpServletResponse resp) throws IOException {

		StringBuilder strBu = new StringBuilder();
		strBu.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBu.append("<response>");
		strBu.append("<handleResult>");
		strBu.append("FAILED");
		strBu.append("</handleResult>");
		strBu.append("<failedCode>");
		strBu.append(resCode);
		strBu.append("</failedCode>");
		strBu.append("<failedReason>");
		strBu.append(resReason);
		strBu.append("</failedReason>");
		strBu.append("</response>");

		String resStr = strBu.toString();
		resp.getWriter().write(resStr);
		resp.getWriter().flush();
		resp.getWriter().close();
	}


	/**
	 * 数据校验失败返回值
	 * 
	 * @param resCode
	 * @param resReason
	 * @param resp
	 * @throws IOException
	 */
	public static void returnResMsg(String resCode, String resReason,
			HttpServletResponse resp) throws IOException {

		StringBuilder strBu = new StringBuilder();
		strBu.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
		strBu.append("<response>");
		strBu.append("<handleResult>");
		strBu.append("FAILED");
		strBu.append("</handleResult>");
		strBu.append("<failedCode>");
		strBu.append(resCode);
		strBu.append("</failedCode>");
		strBu.append("<failedReason>");
		strBu.append(resReason);
		strBu.append("</failedReason>");
		strBu.append("</response>");

		String resStr = strBu.toString();
		resp.getWriter().write(resStr);
		resp.getWriter().flush();
		resp.getWriter().close();
	}

	/**
	 * 失败时返回的错误信息
	 * 
	 * @param resCode
	 * @param resReason
	 * @param resp
	 * @throws IOException
	 */
	public static void returnFailMsg(String resCode, String resReason,
			HttpServletResponse resp) throws IOException {
		StringBuilder builder = new StringBuilder();
		builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		builder.append("<response>");
		builder.append("<resMsg>");
		builder.append("SUCCESS");
		builder.append("</resMsg>");
		builder.append("<failedCode>");
		builder.append(resCode);
		builder.append("</failedCode>");
		builder.append("<failedReason>");
		builder.append(resReason);
		builder.append("</failedReason>");
		builder.append("</response>");

		resp.getWriter().write(builder.toString());
		resp.getWriter().flush();
		resp.getWriter().close();
	}

	/**
	 * 1.新增sup供货商品接口； 2.货源变更接口； 3.交易订单退款通知接口 响应消息xml格式组装
	 * 
	 * @param resBodyStr
	 *            消息体
	 * @param resCode
	 *            响应码
	 * @param resMsg
	 *            响应消息
	 * @return
	 */
	public static String assembleXmlString(String resBodyStr, String resCode,
			String resMsg) {
		StringBuilder strBu = new StringBuilder();
		strBu.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
		strBu.append("<response>");
		strBu.append("<handleResult>");
		if (TransmissionCode.SUCCESS.getCode().equals(resCode)) { // 请求成功
			strBu.append("SUCCESS");
			strBu.append("</handleResult>");

			strBu.append("<failedCode>");
			strBu.append("");
			strBu.append("</failedCode>");
			strBu.append("<failedReason>");
			strBu.append("");
			strBu.append("</failedReason>");
		} else {
			strBu.append("FAILED");
			strBu.append("</handleResult>");
			strBu.append("<failedCode>");
			
			resCode = "201";
			strBu.append(resCode);
			strBu.append("</failedCode>");
			strBu.append("<failedReason>");
			if (resMsg != null && !("".equals(resMsg))) {
				strBu.append(resMsg);
			}else {
				strBu.append("其他错误");
			}
			strBu.append("</failedReason>");
		}
		strBu.append("</response>");

		String xmlStr = strBu.toString();
		return xmlStr;
	}

	/**
	 * 异步充值订单状态变化通知接口,响应消息xml格式组装
	 * 
	 * @param resBodyStr
	 *            消息体
	 * @param resCode
	 *            响应码
	 * @param resMsg
	 *            响应消息
	 * @param ptOrderNo
	 *            合作方平台订单编号
	 * @return
	 */
	public static String assembleXmlStrForOrder(String resBodyStr,
			String resCode, String resMsg, String ptOrderNo) {
		StringBuilder strBu = new StringBuilder();
		strBu.append("<?xml version=\"1.0\" encoding=\"GBK\"?>");
		strBu.append("<response>");
		strBu.append("<ptOrderNo>");
		strBu.append(ptOrderNo);
		strBu.append("</ptOrderNo>");
		strBu.append("<handleResult>");
		if (TransmissionCode.SUCCESS.getCode().equals(resCode)) { // 请求成功
			strBu.append("SUCCESS");
			strBu.append("</handleResult>");
			strBu.append("<failedCode>");
			strBu.append("");
			strBu.append("</failedCode>");
			strBu.append("<failedReason>");
			strBu.append("");
			strBu.append("</failedReason>");
		} else {
			strBu.append("FAILED");
			strBu.append("</handleResult>");
			strBu.append("<failedCode>");
			
			resCode = "201";
			strBu.append(resCode);
			strBu.append("</failedCode>");
			strBu.append("<failedReason>");
			
			try {
				resMsg = URLDecoder.decode(resMsg, "GBK");
			} catch (UnsupportedEncodingException e) {
			}
			strBu.append(resMsg);
			strBu.append("</failedReason>");
		}
		strBu.append("</response>");

		String xmlStr = strBu.toString();
		return xmlStr;
	}
	
	/**
	 * 异步充值订单状态变化通知接口,响应消息xml格式组装
	 * 
	 * @param resBodyStr
	 *            消息体
	 * @param resCode
	 *            响应码
	 * @param resMsg
	 *            响应消息
	 * @param ptOrderNo
	 *            合作方平台订单编号
	 * @return
	 */
	public static String assembleXmlStrAboutXingQiTian(String resBodyStr,
			String resCode, String resMsg){
		StringBuilder strBu = new StringBuilder();
		if (TransmissionCode.SUCCESS.getCode().equals(resCode)){
			strBu.append("<result>");
			strBu.append("OK");
			strBu.append("</result>");
		}else {
			strBu.append("<result>");
			strBu.append("Fail");
			strBu.append("</result>");
		}
		
		String xmlStr = strBu.toString();
		return xmlStr;
	}
	
	/**
	 * 异步充值订单状态变化通知接口,响应消息xml格式组装
	 * @param resBodyStr
	 * @param resCode
	 * @param resMsg
	 * @return
	 */
	public static String assembleXmlStrAboutJieyitong(String resBodyStr,
			String resCode, String resMsg){
		String result = null;
		if (TransmissionCode.SUCCESS.getCode().equals(resCode)){
			result = "ok";
		}else {
			result = "failure";
		}
		
		return result;
	}

}
